import FrameAnimator from "./FrameAnimator";

const { ccclass, property, executeInEditMode, menu, requireComponent } = cc._decorator;

@ccclass
@requireComponent(FrameAnimator)
@menu('cccframework/' + FrameAnimatorMgr.name)
export default class FrameAnimatorMgr extends cc.Component {

    @property(FrameAnimator)
    _curPlayAnimator: FrameAnimator = null;
    @property({ type: FrameAnimator })
    get curPlayAnimator(): FrameAnimator {
        return this._curPlayAnimator;
    };
    set curPlayAnimator(v: FrameAnimator) {
        if (this._curPlayAnimator) {
            this._curPlayAnimator.stop();
        }
        if (v) {
            this.playAnimator(v)
            this._curPlayAnimator = v;
        } else {
            console.error('动画有误', v)
            if (CC_EDITOR) {
                Editor.log('动画有误', v);
            }
            this._curPlayAnimator = null;
        }
    };

    @property(cc.String)
    _animatorName: string = 'start';
    @property({ type: cc.String })
    get animatorName(): string {
        return this._animatorName;
    };
    set animatorName(v: string) {
        this._animatorName = v;
        let list = this.animatorList;
        let res = list.find((animator: FrameAnimator) => {
            return !!animator.animatorName && animator.animatorName == v;
        })
        this.curPlayAnimator = res;
    };

    get animatorList(): FrameAnimator[] {
        let comps = this.node.getComponents('FrameAnimator')
        return comps;
    };


    playAnimator(frameAnimator: FrameAnimator) {
        frameAnimator.play();
    }

}
